package zte.test;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.scheduler.QueueScheduler;
import us.codecraft.webmagic.selector.Selectors;

public class MyQueueScheduler extends QueueScheduler {
	private BlockingQueue<Request> queue = new LinkedBlockingQueue<Request>();

	@Override
	public void pushWhenNoDuplicate(Request request, Task task) {
		HttpClientDownloader downloader = new HttpClientDownloader();
		Page page = downloader.download(request, task);
		System.out.println(page.getHtml().toString());
		List<String> hlist = page.getHtml().xpath("div[@class=\"tn-list\"]/div/div/dl/dd/html()").all();
		if (hlist != null) {
			for (String s : hlist) {
				System.out.println(s);
				String link = Selectors.xpath("a/@href").select(s);
				System.out.println(link);
				String text = Selectors.xpath("a/text()").select(s);
				System.out.println(text);

			}
		}
		/*List<String> hlist = page.getHtml().xpath("div[@class=\"thread-w leftPostCon1 latestReplyDiv\"]/ul/li/html()").all();
		if (hlist != null) {
			for (String s : hlist) {
				//System.out.println(s);
				String link = Selectors.xpath("div/div/a/@href").select(s);
				System.out.println(link);
				String text = Selectors.xpath("div/div/a/html()").select(s);
				System.out.println(text);
				System.out.println(Selectors.regex(".*").select(text));
				
			}
		}
		*/
		
		this.queue.add(request);
	}

	@Override
	public synchronized Request poll(Task task) {
		return this.queue.poll();
	}

	@Override
	public int getLeftRequestsCount(Task task) {
		return this.queue.size();
	}
}
